# sg90 舵机模块
from machine import Pin, PWM

def turn(cycle):
    pwm0 = PWM(Pin(5))  # 从管脚创建PWM对象
    pwm0.freq(50)  # 设定频率
    pwm0.duty(200)  # 设置占空比
    pwm0.duty(int((cycle) / 20 * 1023))  # 设置占空比

def turn_angle(angle):
    if angle == b'WTF':
        return
    a = int(angle)
    print(a)
    if a == 0:
        turn(0.5)
    if a == 45:
        turn(1)
    if a == 90:
        turn(1.5)
    if a == 135:
        turn(2)
    if a == 180:
        turn(2.5)

# PWM高电平占整个周期的时间	舵机旋转角度
# 0.5ms	0°
# 1ms	45°
# 1.5ms	90°
# 2ms	135°
# 2.5ms	180°
